package dji.sampleV5.aircraft.upload

import dji.v5.utils.common.LogUtils
import okhttp3.Call
import okhttp3.Callback
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
import java.io.File
import java.io.IOException
import java.text.SimpleDateFormat

class NetManager {
    private val MEDIA_TYPE_JPG = MediaType.parse("image/jpg")
    private val tag: String = javaClass.simpleName
    private val okHttpClient = OkHttpClient()

    suspend fun uploadFiles(url: String, filePathArray: Array<String>) {
        val filesArray = filePathArray.map { File(it) }
        val builder = MultipartBody.Builder()
                .setType(MultipartBody.FORM)

        //读取txt文档
        //遍历Map，将参数添加到builder中
//        val file: File = File(textName)
//        try {
//            val reader = BufferedReader(FileReader(file))
//            var line: String
//            //String [] args = {"name","x","y","z","phi","omg","kap","timeCreate","timeRec"};
//            while (reader.readLine().also { line = it } != null) {
//                val args0 = line.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0]
//                params.put(args0, line.substring(args0.length + 1))
//            }
//            reader.close()
//        } catch (e: FileNotFoundException) {
//            e.printStackTrace()
//        } catch (e: IOException) {
//            e.printStackTrace()
//        }
        val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val time = sdf.format(System.currentTimeMillis())
        var params = HashMap<String, String>()
        params.put("name","1")
        params.put("x","1")
        params.put("y","1")
        params.put("z","1")
        params.put("phi","1")
        params.put("omg","1")
        params.put("kap","1")
        params.put("timeCreate",time)
        params.put("timeRec",time)
        for ((key, value) in params.entries) {
            builder.addFormDataPart(key, value)
        }

        filesArray.forEachIndexed { index, file ->
            val requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file)
            val requestBody1 = RequestBody.create(MEDIA_TYPE_JPG, file)
            builder.addFormDataPart("file$index", file.name, requestBody)
        }
        val multipartBody = builder.build()
        val request = Request.Builder()
                            .url(url)
                            .post(multipartBody)
                            .build()
        val call = okHttpClient.newCall(request)
        call.enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                LogUtils.e(tag, "init_image_upload fail=${e.message}")
                LogUtils.e("init_image", "init_upload=进入44444444:"+e.message)
            }

            override fun onResponse(call: Call, response: Response) {
                LogUtils.e(tag, "init_image_upload success=${response.code()}")
                LogUtils.e("init_image", "init_upload=进入666666666666666")

//                //删除上传成功文件
//                if (filePathArray[0].exists()) {
//                    filePathArray.delete()
//                }
            }
        })
    }
}